import * as constants from './constants'
import { fromJS } from 'immutable'
    
const defualtState = fromJS({
    focused: false,
    list: [],
    page: 1,
    totalPage: 1,
    mouseIn:false
})

export default (state = defualtState, action) => {
    // if (action.type === constants.SEARCH_FOCUS) {
    //     // immutable 对象的set方法，会结合之前 immutable对象的值和设置的值，返回一个全新的对象

    //     return state.set('focused', true);

    //     // 旧写法：
    //     // return {
    //     //     focused:true
    //     // }
    // }
    // if (action.type === constants.SEARCH_BLUR) {
    //     // 同样使用 immutable来写
    //     return state.set('focused', false);
    // }
    // if (action.type === constants.CHANGE_LIST) {
    //     return state.set('list', action.data);
    // }
    // return state;
    switch (action.type) {
        case constants.SEARCH_FOCUS:
            return state.set('focused', true);
        case constants.SEARCH_BLUR: 
            return state.set('focused', false);
        case constants.CHANGE_LIST:
            // return state.set('list', action.data).set('totalPage', action.totalPage);
            return state.merge({
                list: action.data,
                totalPage:action.totalPage
            })
        case constants.MOUSE_ENTER:
            return state.set('mouseIn', true);
        case constants.MOUSE_LEAVE:
            return state.set('mouseIn', false);
        case constants.CHANGE_PAGE:
            return state.set('page', action.page);
        default:
            return state;
    }
    
}